package com.leaf.zuul.filter;

import javax.servlet.http.HttpServletRequest;

import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

/**
 * 自定义ZuulFilter
 *@Title:  
 *@Description:  
 *@Author: leaf  
 *@Since:2019年7月1日  
 *@Version:1.1.0
 */
@Component
public class LogFilter extends ZuulFilter {

	// 判断该过滤器是否要执行，true 表示执行
	@Override
	public boolean shouldFilter() {
		return true;
	}

	// 过滤器的具体逻辑
	@Override
	public Object run() throws ZuulException {
		RequestContext currentContext = RequestContext.getCurrentContext();
		HttpServletRequest request = currentContext.getRequest();
		String remoteAddr = request.getRemoteAddr();
		System.out.println("访问者IP：" + remoteAddr + " 访问地址：" + request.getRequestURI());
		return null;
	}

	// 过滤器的类型
	@Override
	public String filterType() {
		return FilterConstants.ROUTE_TYPE;
	}

	// 执行顺序
	@Override
	public int filterOrder() {
		return FilterConstants.PRE_DECORATION_FILTER_ORDER;
	}

}
